package vis.ds.stack;

import processing.core.PApplet;
import vis.app.VisualObject;

public class StackElem extends VisualObject {
	public static int ELEM_SIZE = 60;
	private static int MOVE_SPEED = 3;

	private int initX = 0;
	private int initY = 0;

	private int x = 0;
	private int y = 0;
	private int color;

	private int index = 0;
	private int limitY = 0;
	private int value = 0;

	public StackElem(PApplet parent, int index, int value) {
		super(parent);
		System.out.println("create new StackElem at: " + index);

		this.index = index;
		this.value = value;

		initX = p.width / 2;
		initY = 0;
		x = initX - ELEM_SIZE / 2;

		color = p.color(p.random(255), p.random(255), p.random(255));
		// p.rectMode(p.CENTER);

		calculateLimitY();
	}

	private void calculateLimitY() {
		limitY = p.height - Stack.BOTTOM_MARGIN - (index + 1) * ELEM_SIZE;
	}

	@Override
	public void update() {
		if (limitY > 0 && y < limitY) {
			y += 5;
		}
	}

	@Override
	public void display() {
		p.fill(color);
		{
			// p.noStroke();
			p.rect(x, y, ELEM_SIZE, ELEM_SIZE);
		}
		p.noFill();
	}

}
